import cv2
import numpy as np
from matplotlib import pyplot as plt
import os


def make_files():
    dir_name = "f1"
    out_dir_name = "f6"

    for file_name in os.listdir(dir_name):
        img = cv2.imread("{0}/{1}".format(dir_name, file_name),0)
        # channels = cv2.split(img)
        # cv2.imwrite("{0}/blue_{1}".format(out_dir_name, file_name), channels[2])
        # cv2.imwrite("{0}/gray_{1}".format(out_dir_name, file_name), img)
        bialater = cv2.bilateralFilter(img, 9, 75, 75)
        # kernel = np.ones((5,5),np.uint8)
        kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (9, 9))
        edges = cv2.morphologyEx(bialater, cv2.MORPH_GRADIENT, kernel)

        plt.subplot(121),plt.imshow(img,cmap = 'gray')
        plt.title('Original Image'), plt.xticks([]), plt.yticks([])
        plt.subplot(122),plt.imshow(edges,cmap = 'gray')
        plt.title('Edge Image'), plt.xticks([]), plt.yticks([])

        plt.savefig("{0}/{1}".format(out_dir_name, file_name))


if __name__ == "__main__":
    make_files()
    # img = cv2.imread(("f1/0001.jpg"), 0)
    # # channels = cv2.split(img)
    # bialater = cv2.bilateralFilter(img, 9, 75, 75)
    # image, contours, hierarchy = cv2.findContours(bialater, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
    # # bialater = cv2.bilateralFilter(bialater, 9, 75, 75)
    # # bialater = cv2.bilateralFilter(bialater, 9, 75, 75)
    # # bialater = cv2.bilateralFilter(bialater, 9, 75, 75)
    # # bialater = cv2.bilateralFilter(bialater, 9, 75, 75)
    # kernel = np.ones((5, 5), np.uint8)
    # edges = cv2.drawContours(image, contours, -1, (0, 255, 0), 3)
    # # edges = cv2.morphologyEx(image, cv2.MORPH_GRADIENT, kernel)
    #
    # plt.subplot(121), plt.imshow(img, cmap='gray')
    # plt.title('Original Image'), plt.xticks([]), plt.yticks([])
    # plt.subplot(122), plt.imshow(edges, cmap='gray')
    # plt.title('Edge Image'), plt.xticks([]), plt.yticks([])
    #
    # plt.show()
